var catbox = document.getElementsByClassName("catbox")[0];

var checkAll = document.getElementsByClassName("check-all")[0];
var checkOneList = document.getElementsByClassName("check-one");

var selectedTotal = document.getElementById("selectedTotal");
var priceTotal = document.getElementById("priceTotal");

catbox.addEventListener("click", shoppingCarHandler);

function shoppingCarHandler(e) {
    // this -> 绑定事件的元素  catbox
    // target -> 触发事件的元素 事件源(可以是catbox子代元素中的每一个)
    var e = e || window.event;
    var target = e.target; //事件源(触发事件的元素)
    // console.log(11111);

    if (target.classList.contains("check-all")) {
        // console.log(target);
        checkAllHandler(target);
        getTotal();
    } else if (target.classList.contains("check-one")) {
        isAllChecked();
        getTotal();
    } else if (target.className == "add") {
        console.log(target);
        addHandler(target);
        getTotal();
    } else if (target.className == "reduce") {
        console.log(target);
        reduceHandler(target);
        getTotal();
    } else if (target.className == "delete") {
        console.log(target);
        deleteOneHandler(target);
        isAllChecked();
        getTotal();
    } else if (target.id == "deleteAll") {
        deleteAllHandler();
        isAllChecked();
        getTotal();
    }




}

function checkAllHandler(target) {
    var status = target.checked;
    console.log(status);
    for (var i = 0; i < checkOneList.length; i++) {
        var checkOne = checkOneList[i];
        checkOne.checked = status;
    }
}

function isAllChecked() {
    var flag = Array.from(checkOneList).every(function (item, index, array) {
        return item.checked == true;
    })
    console.log(flag);
    checkAll.checked = checkOneList.length > 0 ? flag : false;
}

function addHandler(target) {
    // a. 输入框  数量 加一 
    var countInput = target.previousElementSibling;
    var num = countInput.value; // countInput的value值  num =  "1"
    num++;
    countInput.value = num;
    // countInput.value++;

    // b. 小计更新  =  单价*数量
    var countTd = target.parentElement.previousElementSibling;
    var subtotalTd = target.parentElement.nextElementSibling;

    // 注意:如果有单位  "5999.88元"  "¥5999.88"  => 处理(去单位)
    var price = countTd.textContent;

    var subtotal = price * num;

    subtotalTd.textContent = subtotal.toFixed(2);

    // 3. 减号按钮的 “-”  出现
    var reduce = countInput.previousElementSibling;
    reduce.textContent = "-";
}

function reduceHandler(target) {
    // a. 输入框  数量 减一 
    var countInput = target.nextElementSibling;
    var num = countInput.value;
    // c .减之前  判断  如果数量为1  不能减     return false
    if (num <= 1) { return false };

    num--; //自减

    //d. 减之后  判断  如果数量为1(2切为1)  减号按钮的 “-” 消失
    if (num <= 1) {
        target.textContent = "";
    }

    countInput.value = num;

    var countTd = target.parentElement.previousElementSibling;
    var subtotalTd = target.parentElement.nextElementSibling;

    // b. 小计更新
    // 注意:如果有单位  "5999.88元"  "¥5999.88"  => 处理(去单位)
    var price = countTd.textContent;

    var subtotal = price * num;

    subtotalTd.textContent = subtotal.toFixed(2);
}

function deleteOneHandler(target) {
    if (confirm("是否删除该商品?")) {
        var tr = target.parentElement.parentElement;
        tr.remove();
    }
}

function deleteAllHandler() {
    for (var j = 0; j < checkOneList.length; j++) {
        var checkOne = checkOneList[j];
        if (checkOne.checked) {
            var tr = checkOne.parentElement.parentElement;
            tr.remove();
            j--;
        }
    }
}

function getTotal() {
    var sum = 0; // 被选中的商品的数量
    var allPrice = 0; // 被选中的商品的总价格

    // 找到所有选中状态的单选  => 通过它找到该商品对应的数量和小计  => 累加
    for (let j = 0; j < checkOneList.length; j++) {
        var item = checkOneList[j];
        if (item.checked) {
            var tr = item.parentElement.parentElement;  // 选中状态的单选 => tr
            var countInput = tr.getElementsByClassName("count-input")[0];
            var subtotalTd = tr.getElementsByClassName("subtotal")[0];
            // console.log(countInput, subtotalTd);

            var num = countInput.value * 1;
            var subtotal = subtotalTd.textContent * 1;

            sum += num;
            allPrice += subtotal;

        }
    }

    selectedTotal.textContent = sum;
    priceTotal.textContent = allPrice.toFixed(2);
}